package com.loong.web.gp4j.service.message;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.loong.web.gp4j.bean.schema.message.UserMessageSchema;
import com.loong.web.gp4j.bean.vo.message.UserMessageVo;
import com.loong.web.gp4j.dao.message.UserMessageCache;
import com.loong.web.gp4j.dao.message.UserMessageDao;
import com.loong.web.gp4j.service.error.ErrorService;
import com.loong.web.suppert.auth.User;
import com.loong.web.suppert.auth.UserSessions;
import com.loong.web.suppert.collection.param.PageParam;
import com.loong.web.suppert.collection.param.ParamMap;
import com.loong.web.suppert.collection.vo.ResultVo;
import com.loong.web.suppert.collection.vo.ResultVoFactory;
import com.loong.web.suppert.collection.vo.RowsVo;

/**
 * 用户消息服务
 *
 * @author 张成轩
 */
@Service
public class UserMessageService {

	@Resource
	private UserMessageDao userMessageDao;
	@Resource
	private UserMessageCache userMessageCache;
	@Resource
	private ErrorService errorService;

	/**
	 * 获取未读用户消息数量
	 * 
	 * @param request 请求
	 * @return 用户未读消息数量
	 */
	public int getUserMessageCount(HttpServletRequest request) {

		User user = UserSessions.getUser(request);
		Integer count = null;
		// redis是否异常
		boolean redisEx = true;
		try {
			// 获取用户消息数量
			count = userMessageCache.get(user.getUser());
		} catch (RuntimeException e) {
			redisEx = false;
			errorService.error(e);
		}
		if (count == null) {
			// 查询数量
			count = userMessageDao.getCountByUserIdWithDetail(user.getId(), false);
			// 保存缓存
			if (redisEx) {
				userMessageCache.put(user.getUser(), count);
			}
		}
		return count;
	}

	/**
	 * 获取用户消息列表
	 * 
	 * @param request 请求
	 * @param read 是否已读
	 * @param page 分页对象
	 * @return 分页对象
	 */
	public RowsVo<UserMessageVo> queryUserMessage(HttpServletRequest request, Boolean read, PageParam page) {

		// 封装参数
		User user = UserSessions.getUser(request);
		ParamMap params = new ParamMap();
		params.put("read", read);
		params.put("userId", user.getId());
		params.setPage(page);

		// 查询
		List<UserMessageSchema> messages = userMessageDao.query(params);

		// 封装对象
		List<UserMessageVo> vos = new LinkedList<UserMessageVo>();
		UserMessageAdapter.convert(messages, vos);
		RowsVo<UserMessageVo> vo = new RowsVo<UserMessageVo>();
		vo.setPage(page);
		vo.setRows(vos);
		return vo;
	}

	/**
	 * 标记消息已读
	 * 
	 * @param request 请求
	 * @param id 用户消息id
	 * @return 返回对象
	 */
	public ResultVo<?> readMessage(HttpServletRequest request, Integer id) {

		User user = UserSessions.getUser(request);
		int count = userMessageDao.updateReadTime(user.getId(), id);
		// 返回
		if (count != 1)
			return ResultVoFactory.fail("更新失败");
		// 更新缓存
		int mc = userMessageDao.getCountByUserIdWithDetail(user.getId(), false);
		userMessageCache.put(user.getUser(), mc);
		return ResultVoFactory.succ(mc);
	}
}
